import { ref } from 'vue'
import { defineStore } from 'pinia'

// 用户列表
export const useUserListStore = defineStore('userList', () => {
    const userList = ref(JSON.parse(localStorage.getItem('userList')) || [])

    function setUserList() {
        localStorage.setItem('userList', JSON.stringify(userList.value))
    }

    function addUserList(data) {
        userList.value.push(data)
        setUserList()
    }

    function getUserInfo(userid) {
        for (let i = 0; i < userList.value.length; i++) {
            if (userList.value[i].userid == userid) {
                return userList.value[i]
            }
        }
    }

    function changeInfo(info) {
        for (let i = 0; i < userList.value.length; i++) {
            if (userList.value[i].userid == info.userid) {
                if (info.img) {
                    userList.value[i].img = info.img
                }
                if (info.username) {
                    userList.value[i].username = info.username
                }
                break
            }
        }
        setUserList()
    }

    function addPost(data) {
        for (let i = 0; i < userList.value.length; i++) {
            if (userList.value[i].userid == data.userid) {
                userList.value[i].post.unshift(data.postid)
                break
            }
        }
        setUserList()
    }

    function addComment(data) {
        for (let i = 0; i < userList.value.length; i++) {
            if (userList.value[i].userid == data.userid) {
                userList.value[i].comments.unshift(data.data)
                break
            }
        }
        setUserList()
    }

    function addLike(data) {
        let flag = false
        for (let i = 0; i < userList.value.length; i++) {
            if (userList.value[i].userid == data.userid) {
                if (userList.value[i].likes.indexOf(data.postid) == -1) {
                    userList.value[i].likes.unshift(data.postid)
                    flag = true
                }
            }
        }
        if (flag) {
            for (let i = 0; i < userList.value.length; i++) {
                if (userList.value[i].userid == data.postUserid) {
                    userList.value[i].liked.unshift({
                        postid: data.postid,
                        userid: data.userid
                    })
                    break
                }
            }
        }
        setUserList()
    }

    function addCollect(data) {
        for (let i = 0; i < userList.value.length; i++) {
            if (userList.value[i].userid == data.userid) {
                if (userList.value[i].collects.indexOf(data.postid) == -1) {
                    userList.value[i].collects.unshift(data.postid)
                }
            }
        }
        setUserList()
    }

    function addConcern(data) {
        let flag = false
        for (let i = 0; i < userList.value.length; i++) {
            if (userList.value[i].userid == data.userid) {
                if (userList.value[i].concern.indexOf(data.concernUserid) == -1) {
                    userList.value[i].concern.unshift(data.concernUserid,)
                    flag = true
                }
            }
        }
        if (flag) {
            for (let i = 0; i < userList.value.length; i++) {
                if (userList.value[i].userid == data.concernUserid) {
                    if (userList.value[i].fans.indexOf(data.userid) == -1) {
                        userList.value[i].fans.unshift(data.userid)
                    }
                }
            }
        }
        setUserList()
        return flag
    }

    return {
        userList,
        addUserList,
        getUserInfo,
        changeInfo,
        addPost,
        addComment,
        addLike,
        addCollect,
        addConcern,
    }
})

// 注册信息验证
export const useRegisterInfoStore = defineStore('registerInfo', () => {
    const registerInfo = ref(JSON.parse(sessionStorage.getItem('registerInfo')) || {
        tel: '',
        code: '',
        checked: false,
    })

    function setInfo() {
        sessionStorage.setItem('registerInfo', JSON.stringify(registerInfo.value))
    }

    function setTel(tel) {
        registerInfo.value.tel = tel
        setInfo()
    }

    function setCode(code) {
        registerInfo.value.code = code
        setInfo()
    }

    function setChecked(flag) {
        registerInfo.value.checked = flag
        setInfo()
    }

    function clear() {
        registerInfo.value = {
            tel: '',
            code: '',
            checked: false,
        }
        setInfo()
    }

    return {
        registerInfo,
        setTel,
        setCode,
        setChecked,
        clear,
    }
})

// 登录信息
export const useUserStore = defineStore('counter', () => {
    const userInfo = ref(JSON.parse(localStorage.getItem('userInfo')) || {
        tel: '',
        password: '',
        userid: '',
        username: '',
        img: '',
        likes: [],      // 点赞
        collects: [],   // 收藏
        historys: [],   // 浏览历史
        concern: [],    // 关注
        post: [],       // 发布
        fans: [],       // 粉丝
        liked: [],      // 被点赞
        comments: [],   // 评论
    })

    function setUserInfo() {
        localStorage.setItem('userInfo', JSON.stringify(userInfo.value))
    }

    function changeUserInfo(data) {
        userInfo.value = { ...data }
        setUserInfo()
    }

    function clear() {
        userInfo.value = {
            tel: '',
            password: '',
            userid: '',
            username: '',
            img: '',
            likes: [],
            collects: [],
            historys: [],
            concern: [],
            post: [],
            fans: [],
        }
        setUserInfo()
    }

    return {
        userInfo,
        changeUserInfo,
        clear,
    }
})
